import { BasicColumn, FormSchema } from '@/components/Table';
import { h } from 'vue';
import { Tag, Switch } from 'ant-design-vue';
import { setMenusStatus } from '@/api/app/setting';
import { useMessage } from '@/hooks/web/useMessage';
import Icon from '@/components/Icon/Icon.vue';
import Dayjs from 'dayjs';

export const columns: BasicColumn[] = [
  // {
  //   title: 'ID',
  //   dataIndex: 'id',
  //   width: 100,
  //   fixed: 'left',
  // },
  {
    title: '菜单名称',
    dataIndex: 'menu_name',
    width: 200,
    align: 'left',
  },
  // {
  //   title: '图标',
  //   dataIndex: 'icon',
  //   width: 50,
  //   customRender: ({ record }) => {
  //     return h(Icon, { icon: record.icon });
  //   },
  // },
  {
    title: '接口路径',
    dataIndex: 'api_url',
    width: 200,
    customRender: ({ record }) => {
      return record.methods ? '[' + record.methods + ']  ' + record.api_url : record.api_url;
    },
  },
  {
    title: '前端权限',
    dataIndex: 'unique_auth',
    width: 180,
  },
  {
    title: '页面路由',
    dataIndex: 'menu_path',
  },
  {
    title: '排序',
    dataIndex: 'sort',
    width: 50,
  },
  {
    title: '状态',
    dataIndex: 'is_show',
    width: 120,
    customRender: ({ record }) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.is_show === 1,
        checkedChildren: '显示',
        unCheckedChildren: '隐藏',
        loading: record.pendingStatus,
        onChange(checked: boolean) {
          record.pendingStatus = true;
          const newStatus = checked ? 1 : 0;
          const { createMessage } = useMessage();
          setMenusStatus(record.id, newStatus)
            .then(() => {
              record.is_show = newStatus;
              createMessage.success(`修改成功`);
            })
            .catch(() => {
              createMessage.error('修改失败');
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
  {
    title: '创建时间',
    dataIndex: 'create_time',
    width: 180,
    format: (record) => {
      return record ? Dayjs.unix(record).format('YYYY-MM-DD hh:mm:ss') : record;
    },
  },
  {
    title: '更新时间',
    dataIndex: 'update_time',
    width: 180,
    format: (record) => {
      return record ? Dayjs.unix(record).format('YYYY-MM-DD hh:mm:ss') : '';
    },
  },
];

// const isDir = (auth_type: string) => auth_type === '0';
const isMenu = (auth_type: number | string) => auth_type === 1;
const isButton = (auth_type: number | string) => auth_type === 2;

export const searchFormSchema: FormSchema[] = [
  {
    field: 'keyword',
    label: '按钮名称',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    field: 'is_show',
    label: '规则状态',
    component: 'Select',
    componentProps: {
      options: [
        { label: '停用', value: '0' },
        { label: '启用', value: '1' },
      ],
    },
    colProps: { span: 8 },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'id',
    label: 'id',
    component: 'Input',
    defaultValue: 0,
    show: false,
  },
  {
    field: 'auth_type',
    label: '菜单类型',
    component: 'RadioButtonGroup',
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '菜单', value: 1 },
        { label: '按钮', value: 2 },
      ],
    },
    colProps: { lg: 24, md: 24 },
  },
  {
    field: 'menu_name',
    label: '菜单名称',
    component: 'Input',
    required: true,
  },
  {
    field: 'name_path',
    label: '菜单别名',
    component: 'Input',
    required: true,
  },
  {
    field: 'pid',
    label: '上级菜单',
    component: 'TreeSelect',
    componentProps: {
      placeholder: '顶级菜单',
      fieldNames: {
        label: 'menu_name',
        key: 'id',
        value: 'id',
      },
      getPopupContainer: () => document.body,
    },
  },
  {
    field: 'menu_path',
    label: '路由地址',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => isMenu(values.auth_type),
  },
  {
    field: 'unique_auth',
    label: '权限标识',
    component: 'Input',
    required: true,
    //ifShow: ({ values }) => isMenu(values.auth_type),
  },
  {
    field: 'component',
    label: '视图',
    component: 'Input',
    defaultValue: '',
    helpMessage: '如父节点等没有视图的菜单不需要填写',
    componentProps: {
      addonBefore: 'views/',
    },
    ifShow: ({ values }) => isMenu(values.auth_type),
  },
  {
    field: 'icon',
    label: '图标',
    component: 'IconPicker',
    defaultValue: '',
    ifShow: ({ values }) => isMenu(values.auth_type),
  },
  {
    field: 'methods',
    label: '请求方式',
    component: 'Select',
    required: true,
    defaultValue: '',
    componentProps: {
      options: [
        { label: '请选择请求方式', value: '' },
        { label: 'GET', value: 'GET' },
        { label: 'POST', value: 'POST' },
        { label: 'PUT', value: 'PUT' },
        { label: 'DELETE', value: 'DELETE' },
      ],
    },
    ifShow: ({ values }) => isButton(values.auth_type),
  },
  {
    field: 'api_url',
    label: '接口地址',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => isButton(values.auth_type),
  },
  {
    field: 'sort',
    label: '排序',
    component: 'InputNumber',
    defaultValue: 0,
  },
  {
    field: 'is_redirect',
    label: '重定向',
    component: 'RadioButtonGroup',
    defaultValue: 0,
    helpMessage: '重定向不需要填写视图',
    componentProps: {
      options: [
        { label: '否', value: 0 },
        { label: '是', value: 1 },
      ],
    },
    ifShow: ({ values }) => isMenu(values.auth_type),
  },
  {
    field: 'redirect',
    component: 'Input',
    label: '重定向地址',
    required: true,
    colProps: {
      span: 8,
    },
    ifShow: ({ values }) => {
      return !!values.is_redirect && isMenu(values.auth_type);
    },
  },
  {
    field: 'remarks',
    label: '备注',
    component: 'InputTextArea',
    colProps: {
      lg: 32,
      md: 24,
    },
    componentProps: {
      rows: 4,
    },
  },
  {
    field: 'is_show',
    label: '状态',
    component: 'RadioButtonGroup',
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '启用', value: 1 },
        { label: '禁用', value: 0 },
      ],
    },
  },
  {
    field: 'is_show_path',
    label: '是否为隐藏菜单',
    component: 'RadioButtonGroup',
    defaultValue: 0,
    componentProps: {
      options: [
        { label: '否', value: 0 },
        { label: '是', value: 1 },
      ],
    },
    ifShow: ({ values }) => isMenu(values.auth_type),
  },
];
